import { getToken, setToken, removeToken, setTimeStamp } from "@/utils/auth";
// import { set } from "nprogress";
import { login, getUserInfo,getUserDetailById } from "@/api/user";
const state = {
  token: getToken(),
  userInfo:{}
};
const mutations = {
  setToken(state, token) {
    state.token = token;
    setToken(token);
  },
  removeToken(state) {
    state.token = null;
    removeToken();
  },
  setUserInfo(state, userInfo) {
    state.userInfo={...userInfo}
  },
  removeUserInfo(state) {
    state.userInfo={}
  }
};

const actions = {
  async login(ctx, data) {
    
    const result = await login(data);

    ctx.commit("setToken", result);
    setTimeStamp()
  },
  async getUserInfo(ctx) {
    const result = await getUserInfo();
    const baseInfo=await getUserDetailById(result.userId)
    const baseResult={...result, ...baseInfo}
    ctx.commit("setUserInfo", baseResult);
    return baseResult;
  },
  logout(ctx){
    ctx.commit('removeToken')
    ctx.commit('removeUserInfo')
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
